package com.zemb.flxwear.TaskQueue;

import com.zemb.flxwear.Adapter.DeviceAdapter;
import com.zemb.flxwear.Bean.ReportItem;
import com.zemb.flxwear.Utils.LogUtils;

import java.util.List;

public class TaskThread extends Thread {
    TaskQueue taskQueue;
    TaskQueue.OnTaskFinishedListener listener;

    boolean isStop = false;

    public TaskThread(TaskQueue.OnTaskFinishedListener listener) {
        super();
        this.listener = listener;
        taskQueue = new TaskQueue();
    }

    public void addTask(BaseTask task) {
//        锁住taskQueue
        synchronized (taskQueue) {
            taskQueue.addTask(task);
        }

    }


    @Override
    public void run() {
        super.run();
        try {
            while (taskQueue.getCurrentTask() != null) {
                LogUtils.PrintLog(Thread.currentThread().isInterrupted() + "  isInterrupted ");
                if (isStop) {
                    LogUtils.PrintLog("TaskThread", "run: isStop");
                    return;
                }
                listener.onTaskStart(taskQueue.getCurrentTask());
                //等待任务完成
                long time = System.currentTimeMillis();
                while (!taskQueue.getCurrentTask().getFinished()) {
                    if (isStop) {
                        LogUtils.PrintLog("TaskThread", "run: isStop");
                        return;
                    }
                    Thread.sleep(100);
                    //如果超过10分钟，任务失败
                    if (System.currentTimeMillis() - time > 600000) {
                        taskQueue.failTask(taskQueue.getCurrentTask().getType());
                        if (isStop) {
                            LogUtils.PrintLog("TaskThread", "run: isStop");
                            return;
                        }
                        listener.onTaskFailed();
                        break;
                    }
                }
                if (isStop) {
                    LogUtils.PrintLog("TaskThread", "run: isStop");
                    return;
                }
                listener.onTaskFinished();
                taskQueue.next();
                Thread.sleep(5000);

            }
        } catch (Exception e) {
            e.printStackTrace();
            listener.onAllFailed();
            return;
        }
    }

    //完成任务
    public void finishTask(ReportItem item) {
        try {
            synchronized (taskQueue) {
                taskQueue.finishTask(item);
            }
        } catch (Exception e) {
            e.printStackTrace();
            //锁住
        }
    }

    public void failTask() {
        try {
            //锁住
            synchronized (taskQueue) {
                taskQueue.failTask(taskQueue.getCurrentTask().getType());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public void passTask(){
        try {
            //锁住
            synchronized (taskQueue) {
                taskQueue.passTask();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<ReportItem> getResult() {
        return taskQueue.getAllResult();
    }

    public void stopThread() {
        try {
            isStop = true;
            interrupt();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
